Ext.define('Admin.view.sys.DdsMRiverController', {
	extend: 'Ext.app.ViewController',
	alias: 'controller.ddsMRiverController',
	init: function () {},

	//状态管理
	state: {
		// refs: this.getReferences(), //所有的references的集合
	},

	//河流搜索
	riverSearch: function () {
		var refs = this.getReferences();
		// var serchName = refs.serchName.getValue();
		var serchRcd = refs.serchRcd.getValue();
		var grid = refs.riverGrid;
		var params = {
			rcd: serchRcd,
			river: config.RIVER
		};
		grid.getStore().getProxy().extraParams = params;
		grid.getStore().reload();
	},

	//删除
	riverDelete: function (grid, rowIndex, colIndex) {
		var river = grid.getStore().getAt(rowIndex).get('river');
		var rcd = grid.getStore().getAt(rowIndex).get('rcd');
		Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
			if (button === 'yes') {
				Ext.Ajax.request({
					url: config.URL + '/emergency/ddsMRiver/delete',
					method: "post",
					success: function (response, opts) {
						Ext.MessageBox.hide();
						var result = Ext.JSON.decode(response.responseText);
						if (result.code === 1) {
							grid.store.load();
							Ext.Msg.alert('系统提示', "删除成功");
						} else {
							Ext.Msg.alert("系统提示", result.msg);
						}
					},
					failure: function () {
						Ext.Msg.alert('系统提示', '系统出错！');
					},
					params: {
						river: river,
						rcd: rcd,
						river: config.RIVER
					}
				});
			}
		})
	},

	//新增
	riverAdd: function () {
		var _that = this;
		var win = _that.lookupReference('expertAddWindow');
		if (!win) {
			win = Ext.create('Ext.window.Window', {
				title: '新增河流概化河段',
				width: 530,
				layout: 'fit',
				autoScroll: true,
				closeAction: 'hide',
				reference: 'expertAddWindow',
				items: {
					xtype: 'riverForm',
					border: false,
					reference: 'expertAddForm'
				},
				buttons: [{
						text: '保存',
						handler: function () {
							var form = _that.lookupReference('expertAddForm').getForm();
							if (form.isValid()) {
								var modal = this.up('window');
								var expertGrid = _that.lookupReference('riverGrid');
								var store = expertGrid.getStore();
								var values = form.getValues();
								values.river = config.RIVER;
								Ext.Ajax.request({
									url: config.URL + '/emergency/ddsMRiver/save',
									method: "post",
									success: function (response, opts) {
										var result = Ext.JSON.decode(response.responseText);
										Ext.MessageBox.hide();
										if (result.code === 1) {
											Ext.Msg.alert('系统提示', "保存成功");
											store.reload();
											modal.hide();
										} else {
											Ext.Msg.alert("系统提示", result.msg);
										}
									},
									failure: function () {
										Ext.Msg.alert('系统提示', '系统出错！');
									},
									params: values
								});
							} else {
								Ext.Msg.alert("提示", "表单填写不合法！");
							}
						}
					},
					{
						text: '取消',
						handler: function () {
							var modal = this.up('window');
							modal.hide()
						}
					},
				]
			})
			this.getView().add(win)
		}
		win.show();
		this.lookupReference('expertAddForm').getForm().reset();
	},

	//修改
	riverUpdate: function (grid, rowIndex, colIndex) {
		var rec = grid.getStore().getAt(rowIndex);
		var _that = this;
		var win = this.lookupReference('expertUpdataWindow');
		var saveUpdata = function () {
			var form = _that.lookupReference('expertUpdataForm').getForm();
			if (form.isValid()) {
				var modal = this.up('window');
				var emergencyIncidentGrid = _that.lookupReference('riverGrid');
				var store = emergencyIncidentGrid.getStore();
				var values = form.getValues();
				values.river = config.RIVER;
				Ext.Ajax.request({
					url: config.URL + '/emergency/ddsMRiver/update',
					method: "post",
					success: function (response, opts) {
						Ext.MessageBox.hide();
						var result = Ext.JSON.decode(response.responseText);
						if (result.code === 1) {
							Ext.Msg.alert('系统提示', "修改成功");
							store.reload();
							modal.hide();
						} else {
							Ext.Msg.alert("系统提示", result.msg);
						}
					},
					failure: function () {
						Ext.Msg.alert('系统提示', '系统出错！');
					},
					params: values
				});
			} else {
				Ext.Msg.alert('提示', '表单填写不合法！');
			}
		};
		if (!win) {
			win = Ext.create('Ext.window.Window', {
				title: '修改河流概化河段',
				width: 530,
				layout: 'fit',
				autoScroll: true,
				closeAction: 'hide',
				items: {
					xtype: 'riverForm',
					border: false,
					reference: 'expertUpdataForm'
				},
				reference: 'expertUpdataWindow',
				buttons: [{
						text: '保存',
						handler: saveUpdata
					},
					{
						text: '取消',
						handler: function () {
							var modal = this.up('window');
							_that.lookupReference('expertUpdataForm').getForm().reset();
							modal.hide()
						}
					},

				],

			});
			this.getView().add(win);
		}
		this.lookupReference('expertUpdataForm').getForm().reset();
		this.lookupReference('expertUpdataForm').getForm().setValues(rec.data)
		win.show();

	}





});